﻿using Basic.FlowApplication.Model.Actor;
using Basic.FlowModel.AuditEmp;
using Basic.FlowModel.AuditLogItem;
using Basic.FlowRemoteModel;
using Basic.FlowRemoteModel.Application.Model;
using Basic.HrRemoteModel.Emp.Model;

namespace Basic.FlowApplication.Linq
{
    internal static class AuditEmpLinq
    {
        public static WhileFilterParam GetWhileEmpSearchArg ( this NodeAuditEmpSet actor, AuditItemLog log, FlowUser user )
        {
            WhileFilterParam obj = new WhileFilterParam
            {
                CompanyId = user.CompanyId,
                CurDeptId = log.NextDeptId.GetValueOrDefault(user.DeptId)
            };
            if ( ( FlowSpecifyMode.部门 & actor.SpecifyMode ) == FlowSpecifyMode.部门 && ( FlowSpecifyMode.单位 & actor.SpecifyMode ) == FlowSpecifyMode.单位 )
            {
                obj.Range = FilterRange.部门单位;
            }
            else if ( ( FlowSpecifyMode.单位 & actor.SpecifyMode ) == FlowSpecifyMode.单位 )
            {
                obj.Range = FilterRange.单位;
            }
            else
            {
                obj.Range = FilterRange.部门;
            }
            _InitPostTitleAdmin(obj, actor);
            return obj;
        }
        private static void _InitPostTitleAdmin ( WhileFilterParam obj, NodeAuditEmpSet actor )
        {
            if ( ( FlowSpecifyMode.负责人 & actor.SpecifyMode ) == FlowSpecifyMode.负责人 )
            {
                obj.IsLeader = true;
            }
            else if ( ( FlowSpecifyMode.职务 & actor.SpecifyMode ) == FlowSpecifyMode.职务 )
            {
                obj.Title = actor.TitleCode;
            }
            else if ( ( FlowSpecifyMode.岗位 & actor.SpecifyMode ) == FlowSpecifyMode.岗位 )
            {
                obj.Post = actor.PostCode;
            }
        }
        public static ActorSearchArg GetEmpSearchArg ( this NodeAuditEmpSet actor, FlowUser curUser, FlowUser startUser )
        {
            ActorSearchArg obj = new ActorSearchArg
            {
                CompanyId = actor.CompanyId
            };
            if ( ( FlowSpecifyMode.当前发起人 & actor.SpecifyMode ) == FlowSpecifyMode.当前发起人 )
            {
                _InitDeptUnit(obj, actor, curUser);
            }
            else if ( ( FlowSpecifyMode.流程发起人 & actor.SpecifyMode ) == FlowSpecifyMode.流程发起人 )
            {
                if ( actor.SpecifyMode == FlowSpecifyMode.流程发起人 )
                {
                    obj.EmpId = curUser.EmpId;
                }
                else
                {
                    _InitDeptUnit(obj, actor, startUser);
                }
            }
            else if ( ( FlowSpecifyMode.部门 & actor.SpecifyMode ) == FlowSpecifyMode.部门 )
            {
                obj.DeptId = actor.DeptId;
                _InitPostTitleAdmin(obj, actor);
            }
            else if ( ( FlowSpecifyMode.单位 & actor.SpecifyMode ) == FlowSpecifyMode.单位 )
            {
                obj.UnitId = actor.UnitId;
                _InitPostTitleAdmin(obj, actor);
            }
            else
            {
                _InitPostTitleAdmin(obj, actor);
            }
            return obj;
        }
        private static void _InitPostTitleAdmin ( ActorSearchArg obj, NodeAuditEmpSet actor )
        {
            if ( ( FlowSpecifyMode.负责人 & actor.SpecifyMode ) == FlowSpecifyMode.负责人 )
            {
                obj.IsLeader = true;
            }
            else if ( ( FlowSpecifyMode.职务 & actor.SpecifyMode ) == FlowSpecifyMode.职务 )
            {
                obj.Title = actor.TitleCode;
            }
            else if ( ( FlowSpecifyMode.岗位 & actor.SpecifyMode ) == FlowSpecifyMode.岗位 )
            {
                obj.Post = actor.PostCode;
            }
        }
        private static void _InitDeptUnit ( ActorSearchArg obj, NodeAuditEmpSet actor, FlowUser user )
        {
            if ( ( FlowSpecifyMode.公司 & actor.SpecifyMode ) == FlowSpecifyMode.公司 )
            {
                obj.CompanyId = user.CompanyId;
            }
            else if ( ( FlowSpecifyMode.上级部门 & actor.SpecifyMode ) == FlowSpecifyMode.上级部门 )
            {
                obj.DeptId = user.DeptId;
                obj.Change = SearchRange.上级;
            }
            else if ( ( FlowSpecifyMode.下级部门 & actor.SpecifyMode ) == FlowSpecifyMode.下级部门 )
            {
                obj.DeptId = user.DeptId;
                obj.Change = SearchRange.下级;
            }
            else if ( ( FlowSpecifyMode.部门 & actor.SpecifyMode ) == FlowSpecifyMode.部门 )
            {
                obj.DeptId = user.DeptId;
            }
            else if ( ( FlowSpecifyMode.上级单位 & actor.SpecifyMode ) == FlowSpecifyMode.上级单位 )
            {
                obj.UnitId = user.UnitId;
                obj.Change = SearchRange.上级;
            }
            else if ( ( FlowSpecifyMode.下级单位 & actor.SpecifyMode ) == FlowSpecifyMode.下级单位 )
            {
                obj.UnitId = user.UnitId;
                obj.Change = SearchRange.下级;
            }
            else if ( ( FlowSpecifyMode.单位 & actor.SpecifyMode ) == FlowSpecifyMode.单位 )
            {
                obj.UnitId = user.UnitId;
            }

            _InitPostTitleAdmin(obj, actor);
        }
    }
}
